package com.example.dell.case02_16110100415;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.dell.case02_16110100415.Bean.ErrorBean;
import com.example.dell.case02_16110100415.Retrofit.RetrofitService;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SignUpActivity extends AppCompatActivity {
    private EditText userNameEdit,
            passwordEdit,telEdit,emailEdit;
    private String sessionID, mod,page;
    private RadioButton studentBox,teacherBox;
    private Button signupButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        userNameEdit =
                findViewById(R.id.editText_username);
        passwordEdit =
                findViewById(R.id.editText_password);
        telEdit =
                findViewById(R.id.editText_tel);
        emailEdit =
                findViewById(R.id.editText_email);
        teacherBox =
                findViewById(R.id.radio_teacher);
        studentBox =
                findViewById(R.id.radio_student);
        signupButton =
                findViewById(R.id.button_singup);

        signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username =
                        userNameEdit.getText().toString();

                String password =
                        passwordEdit.getText().toString();

                String tel =
                        telEdit.getText().toString();
                String email =
                        emailEdit.getText().toString();

                boolean isChecked = teacherBox.isChecked();
                String roleid = "3";

                if(!isChecked){
                    roleid = "2";
                }

                if(!username.equals("")&&!password.equals("")&&!tel.equals("")&&!email.equals("")){

                    Retrofit retrofit=new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/userLogin.do  ").build();

                    RetrofitService retrofitService = retrofit.create(RetrofitService.class);

                    Call<ResponseBody> call = retrofitService.signIn(username,password,tel,email,roleid);

                    call.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            try {
                                byte[] data= response.body().bytes();
                                String text= new String(data);

                                if(text.equals("1")){                //注册成功返回1
                                    finish();
                                    Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
                                }
                                else {                               //注册失败返回JSON字符串
                                    ObjectMapper objectMapper =
                                            new ObjectMapper();

                                    ErrorBean errorBean = objectMapper.readValue(text,ErrorBean.class);
                                    //转换成Java对象
                                    Toast.makeText(SignUpActivity.this, errorBean.getError(), Toast.LENGTH_SHORT).show();
                                    //输出错误提示
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {

                        }
                    });
                }
                else {
                    Toast.makeText(SignUpActivity.this, "预期之外的错误", Toast.LENGTH_SHORT).show();
                }

                //
            }
        });

    }
}